package br.ufrj.neg.evento.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.evento.bean.CustoContatoEventoBean;
import br.ufrj.neg.evento.bean.CustoGeralBean;
import br.ufrj.neg.evento.model.CustosDAO;

public class CarregarCustosEventoAction extends VelocityViewServlet {

		/**
	 * 
	 */
	private static final long serialVersionUID = -1975552875019621160L;

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/json; charset:utf-8");
		response.setCharacterEncoding("utf-8");

		loadAll(request,response);

	}


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}
	
	private void loadAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		PrintWriter out = response.getWriter(); 
		
		Integer idEvento = Integer.parseInt(request.getParameter("idEvento"));

		List<CustoContatoEventoBean> custosP = CustosDAO.loadParticipantes(idEvento);
		List<CustoGeralBean> custosG = CustosDAO.loadCustosGerais(idEvento);
		
		
		JSONArray custosParticipantes  = JSONArray.fromObject(custosP);
		JSONArray custosGerais = JSONArray.fromObject(custosG);
		
		JSONObject jsonOut = new JSONObject();
		
		jsonOut.put("participantes",custosParticipantes);
		jsonOut.put("gerais",custosGerais);
		
		out.print(jsonOut);
	
	}
	
}
